<template>
  <div class="app-container">
    <div class="filter-container" style="height: 60px;">
      <span class="filter-container-right">
        <el-button v-permission= "{ permissionCode: 'businessGroup:add ' }" type="primary" size="medium" icon="el-icon-plus" @click="addBusinessGroup">新增</el-button>
        <el-button v-permission= "{ permissionCode: 'businessGroup:delete' }" type="danger" size="medium" icon="el-icon-close" @click="deleteBusinessGroups">删除</el-button>
      </span>

    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      border
      highlight-current-row
      @selection-change="handleSelectChange">
      <el-table-column type="selection" width="40"/>
      <el-table-column align="center" label="业务分组名称" prop="name" min-width="150"/>
      <el-table-column align="center" label="描述" prop="description" min-width="200"/>
      <el-table-column align="center" label="创建日期" prop="create_date" min-width="160"/>
      <el-table-column align="center" label="修改日期" prop="modify_date" min-width="160"/>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button v-permission= "{ permissionCode: 'businessGroup:update' }" type="primary" size="mini" icon="el-icon-edit" @click="editBusinessGroup(scope.row)">修改</el-button>
          <el-button v-permission= "{ permissionCode: 'businessGroup:delete' }" type="danger" size="mini" icon="el-icon-delete" @click="deleteBusinessGroup(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title=" isEdit ? '编辑业务' : '新增业务'" :visible.sync="FormVisible" width="600px">
      <el-form ref="ruleForm" :model="businessGroupForm" :rules="rules" >
        <el-form-item :label-width="formLabelWidth" label="业务分组名称" prop="name">
          <el-input v-model.number="businessGroupForm.name" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="描述">
          <el-input v-model="businessGroupForm.description" type="textarea" auto-complete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd()">取 消</el-button>
        <el-button type="primary" @click="isEdit ? confirmEditBusinessGroup('ruleForm') : confirmAddBusinessGroup('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import commonApi from '@/api/commonApi'

export default {
  name: 'BusinessGroupList',
  data() {
    return {
      FormVisible: false,
      isEdit: false,
      list: null,
      listLoading: false,
      rules: {
        name: [
          { required: true, message: '请输入业务分组名称', trigger: 'blur' }
        ]
      },
      delBusinessGroupIds: [],
      formLabelWidth: '120px',
      businessGroupForm: {},
      copybusinessGroupForm: {} // 修改取消时使用
    }
  },
  created() {
    this.getBusinessGroup()
  },
  methods: {
    getBusinessGroup() {
      this.listLoading = true
      commonApi('/dict/queryGroup', 'get').then(response => {
        if (response.code === 1) {
          this.list = response.data
          this.listLoading = false
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleSelectChange(valArr) {
      this.delBusinessGroupIds = []
      for (let i = 0; i < valArr.length; i++) {
        this.delBusinessGroupIds.push(valArr[i].id)
      }
    },
    addBusinessGroup() {
      this.isEdit = false
      this.FormVisible = true
      for (const index in this.businessGroupForm) {
        this.businessGroupForm[index] = null
      }
    },
    confirmAddBusinessGroup(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.FormVisible = false
          this.listLoading = true
          commonApi('/dict/addGroup', 'post', this.businessGroupForm).then(response => {
            if (response.code === 1) {
              this.businessGroupForm = {}
              this.listLoading = false
              this.getBusinessGroup()
            }
          }).catch(() => {
            this.listLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancelAdd() {
      this.FormVisible = false
      if (!this.isEdit) {
        for (const index in this.businessGroupForm) {
          this.businessGroupForm[index] = null
        }
      } else {
        this.businessGroupForm = this.copybusinessGroupForm
      }
    },
    deleteBusinessGroup(row) {
      this.$confirm(`确定要删除业务分组：${row.name} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delBusinessGroupIds.push(row.id)
        this.deleteBusinessGroupsInterim()
      }).catch(() => {
      })
    },
    deleteBusinessGroups() {
      if (this.delBusinessGroupIds.length === 0) {
        this.$message({
          message: '请选择需要删除业务分组 ！',
          type: 'error'
        })
        return
      }
      this.$confirm(`确定要批量删除业务分组 ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.deleteBusinessGroupsInterim()
      }).catch(() => {
      })
    },
    deleteBusinessGroupsInterim() {
      this.listLoading = true
      commonApi('/dict/deleteGroup', 'post', this.delBusinessGroupIds).then(response => {
        if (response.code === 1) {
          this.$message({
            message: '业务分组删除完成',
            type: 'success'
          })
          this.listLoading = false
          this.delBusinessGroupIds = []
          this.getBusinessGroup()
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    editBusinessGroup(row = {}) {
      this.FormVisible = true
      this.isEdit = true
      this.businessGroupForm = row
      this.copybusinessGroupForm = row
    },
    confirmEditBusinessGroup(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.FormVisible = false
          this.listLoading = true
          commonApi('/dict/updateGroup', 'post', { id: this.businessGroupForm.id, name: this.businessGroupForm.name,
            description: this.businessGroupForm.description }).then(response => {
            if (response.code === 1) {
              this.businessGroupForm = {}
              this.listLoading = false
              this.getBusinessGroup()
            }
          }).catch(() => {
            this.listLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">

</style>
